package com.itshare.video.service.impl;

import com.itshare.video.common.core.constant.enums.ApiInfo;
import com.itshare.video.common.dto.Result;
import com.itshare.video.common.exception.NoDataException;
import com.itshare.video.mapper.VideoMapper;
import com.itshare.video.mapper.custom.VideoMapperCustom;
import com.itshare.video.pojo.bo.VideoPermitBO;
import com.itshare.video.pojo.code.PermitStatus;
import com.itshare.video.pojo.dto.UserTokenDTO;
import com.itshare.video.pojo.po.UserMessage;
import com.itshare.video.pojo.po.Video;
import com.itshare.video.pojo.query.VideoQueryCondition;
import com.itshare.video.service.ManagerService;
import com.itshare.video.util.MessageUtil;
import com.itshare.video.util.mq.UserMessageMq;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @Author lyr
 * @create 2020/10/31 19:51
 */
@Service
public class ManagerServiceImpl implements ManagerService {
    @Resource
    private VideoMapperCustom videoMapperCustom;

    @Resource
    private VideoMapper videoMapper;
    @Resource
    UserMessageMq userMessageMq;
    /**
     * 获取没有被允许的视频
     *
     * @return
     */
    @Override
    public List<Video> getAllVideoInfoNotPermited() {
        VideoQueryCondition videoQueryCondition = VideoQueryCondition
                .builder()
                .permitStatus(0).build();
        return videoMapperCustom.selectVideoByListByCondition(videoQueryCondition);
    }


    @Override
    public Result<?> setVideoPermited(Long videoId, VideoPermitBO videoPermitBO, UserTokenDTO userTokenDTO) {
        Video video = videoMapper.selectByPrimaryKey(videoId);
        if(video==null) {
            throw new NoDataException(ApiInfo.NO_RESOURCE);
        }
        //获取消息，发送到消息队列
        UserMessage message = MessageUtil.videoPermitMsg(videoPermitBO.getPermitStatus(),video.getUserId(),videoPermitBO.getReason());
        //发送消息
        userMessageMq.sentMsg(message);
        if(videoPermitBO.getPermitStatus()==0) {
            //更新
            video.setPermitStatus(PermitStatus.no_permit.type);
        }else{
            video.setPermitStatus(PermitStatus.permitted.type);

        }
        videoMapper.updateByPrimaryKeySelective(video.withUpdatedTime(new Date()).withUpdatedBy(userTokenDTO.getId().toString()));

        return Result.of(ApiInfo.OK);
    }
}
